package com.mcxx.modules.temporary.family.service.write;

import cn.hutool.core.util.IdUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.constant.PropertiesConstant;
import com.mcxx.ansync.event.PropertiesEventHandler;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilyThreeMemberDao;
import com.mcxx.modules.miniSalvation.family.dto.FamilyThreeMemberDTO;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainEntity;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMemberReadService;
import com.mcxx.modules.temporary.order.dto.ApplyRtnDTO;
import com.mcxx.modules.temporary.family.dao.write.TmpFamilyMainWriteDao;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMainEntity;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@TaTransactional
@EnableAsync
public class TmpFamilyMainWriteService {
    @Autowired
    private TmpFamilyMainWriteDao familyMainWriteDao;
    @Autowired
    private TmpFamilyMemberReadService familyMemberReadService;
    @Autowired
    private TmpFamilyMemberWriteService familyMemberWriteService;
    @Autowired
    private PropertiesEventHandler propertiesEventHandler;
    @Autowired
    private FamilyThreeMemberDao familyThreeMemberDao;


    public int addFamily(TmpFamilyMainEntity familyMain) {
        return familyMainWriteDao.insertIgnoreNull(familyMain);
    }


    public int updateFamily(TmpFamilyMainEntity familyMain) {
        propertiesEventHandler.publishChangeEvent(familyMain, PropertiesConstant.CATE_FAMILY_CHANGE, null);
        return familyMainWriteDao.updateIgnoreNull(familyMain);
    }


    public ApplyRtnDTO addFamilyAndMember4Apply(TmpFamilyMainEntity family, TmpFamilyMemberEntity member) {

        if (CommonUtil.invalidArgs(family) && CommonUtil.invalidArgs(member)) {
            return null;
        }
        ApplyRtnDTO rtnDTO = new ApplyRtnDTO();


        Map<String,String> touple = familyMemberReadService.getMemberIdAndFamilyIdByIdCard(member.getIdcard());
        String existedFamilyId = "", existedMasterId = "", memberState = "";

        if (!CommonUtil.invalidArgs(touple)) {
            existedFamilyId = touple.get("familyId");
            existedMasterId = touple.get("memberId");
        }


        if (CommonUtil.invalidArgs(existedFamilyId)) {

            family.setId(IdUtil.simpleUUID());
            addFamily(family);
        } else {

            family.setId(existedFamilyId);
            updateFamily(family);
        }

        FamilyThreeMemberDTO familyThreeMemberDTO  = familyThreeMemberDao.queryMemberByCardId(member.getIdcard());
        if(!CommonUtil.invalidArgs(familyThreeMemberDTO)){
            member.setHelpPoorFlag("1");
            member.setRuralObjectType(familyThreeMemberDTO.getRuralObjectType());
        }


        member.setFamilyId(family.getId());
        if (CommonUtil.invalidArgs(existedMasterId)) {

            member.setId(IdUtil.simpleUUID());
            familyMemberWriteService.addFamilyMember(member);
        } else {

            member.setId(existedMasterId);
            familyMemberWriteService.updateFamilyMember(member);
        }


//        familyMemberWriteService.updateFamilyRelation(member.getFamilyId(), member.getIdcard());

        familyMemberWriteService.updateFamilyApply(member.getFamilyId(), member.getIdcard());


        TmpFamilyMemberEntity memberEntity = familyMemberReadService.getById(member.getId());
        rtnDTO.setMasterInfoComp(StringUtils.isNotEmpty(memberEntity.getCensusType()) ? Constant.MasterInfoComp.YES : Constant.MasterInfoComp.No);


        rtnDTO.setFamilyId(family.getId());
        rtnDTO.setMemberId(member.getId());
        return rtnDTO;
    }


    public void updateFamilyInfo(TmpFamilyMemberDTO familyMemberDTO, UserAccountVo accountVo, Map<String,Integer> countMap) {
        TmpFamilyMainEntity familyMainEntity = new TmpFamilyMainEntity();
        familyMainEntity.setId(familyMemberDTO.getFamilyId());
        familyMainEntity.setOrgId(accountVo.getOrgId());
        familyMainEntity.setFamilyNumber(countMap.get("memberCount"));
        familyMainEntity.setSaveMemberNumber(countMap.get("saveMemberCount"));

        if (Constant.DomesticRelation.ONESELF.equals(familyMemberDTO.getFamilyRelation())){
            familyMainEntity.setName(familyMemberDTO.getName());
            familyMainEntity.setIdcard(familyMemberDTO.getIdcard());
        }

        int rst = updateFamily(familyMainEntity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void copyFromOtherSource(String familyId,String newFamilyId) {
        familyMainWriteDao.copyFromOtherSource(familyId,newFamilyId);
    }


    public void copyFromSplittingSource(String familyId,String newFamilyId,String idcard) {
        familyMainWriteDao.copyFromSplittingSource(familyId,newFamilyId,idcard);
    }


    public void updateFamilyInfo(TmpAuditDTO tmpAuditDTO, UserAccountVo userAccountVo){
        TmpFamilyMainEntity familyMainEntity = new TmpFamilyMainEntity();
        familyMainEntity.setId(tmpAuditDTO.getFamilyId());
        familyMainEntity.setMasterInSave(tmpAuditDTO.getMasterInSave());
        familyMainEntity.setSaveMemberNumber(tmpAuditDTO.getSaveMemberNumber());
        familyMainEntity.setUpdateTime(new Date());
        familyMainEntity.setUpdateBy(userAccountVo.getUserId());
        int rst = familyMainWriteDao.updateIgnoreNull(familyMainEntity);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }

    public int changeSaveMemberById(TmpFamilyMainEntity familyMain,String change){
        return familyMainWriteDao.changeSaveMemberById(familyMain.getId(),familyMain.getMasterInSave(),change);
    }


    public int updateSaveMemberByFamilyId(String id){
        return familyMainWriteDao.updateSaveMemberByFamilyId(id);
    }


    public int copyFromLowerSource(String familyId,String newFamilyId){
        return familyMainWriteDao.copyFromLowerSource(familyId,newFamilyId);
    }


    public int copyFromLowerSplitSource(String familyId,String newFamilyId){
        return familyMainWriteDao.copyFromLowerSplitSource(familyId,newFamilyId);
    }

    public int updateFamilyByFamilyId(FamilyMainEntity family) {
        return familyMainWriteDao.updateFamilyByFamilyId(family);
    }



    public int updateTempFamilyInfo(List<String> familyIds) {
        return familyMainWriteDao.updateTempFamilyInfo(familyIds);
    }



    public int deleteFamilyMainByIdCard(String idCard) {
        return familyMainWriteDao.deleteFamilyMainByIdCard(idCard);
    }

    public int deleteFamilyMainByIdCardAndNoHasFamilyId(String idCard, String familyId) {
        return familyMainWriteDao.deleteFamilyMainByIdCardAndNoHasFamilyId(idCard,familyId);
    }
}
